<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\T_PM_MO_BASE;
use app\model\T_QM_FIRST_INFO;
use app\model\T_QM_IPQC_DOC;
use think\Request;

class Workshopdaily
{
    /**
     * 显示资源列表
     *工单情况汇总
     * @return \think\Response
     */
    public function Ts()
    {
        //开干
        //获取smt、dip、总装的待生产制令单单号和状态
        //待生产
        //smt
        $smt=(new T_PM_MO_BASE())->where([['PRODUCT_STEP','like','%SMT%'],['STATUS','=',1],['PLAN_START_TIME','>=',date('Y-m-d 00:00:00')],['PLAN_START_TIME','<=',date('Y-m-d 23:59:59')]])->field('MO_DOC,STATUS')->select();
        $smt['data']=$smt->toArray();
        $smt['data']=$this->status($smt['data']);
        $smt['count']=count($smt['data']);
        //dip
        $dip=(new T_PM_MO_BASE())->where([['PRODUCT_STEP','like','%THT%'],['STATUS','=',1],['PLAN_START_TIME','>=',date('Y-m-d 00:00:00')],['PLAN_START_TIME','<=',date('Y-m-d 23:59:59')]])->field('MO_DOC,STATUS')->select();
        $dip['data']=$dip->toArray();
        $dip['data']=$this->status($dip['data']);
        $dip['count']=count($dip['data']);
        //总装
        $zz=(new T_PM_MO_BASE())->where([['PRODUCT_STEP','like','%总装%'],['STATUS','=',1],['PLAN_START_TIME','>=',date('Y-m-d 00:00:00')],['PLAN_START_TIME','<=',date('Y-m-d 23:59:59')]])->field('MO_DOC,STATUS')->select();
        $zz['data']=$zz->toArray();
        $zz['data']=$this->status($zz['data']);
        $zz['count']=count($zz['data']);
        //今日已完成
        //smt
        $smts=(new T_PM_MO_BASE())->where([['PRODUCT_STEP','like','%SMT%'],['STATUS','in',[3,5]],['CLOSE_TIME','>=',date('Y-m-d 00:00:00')],['CLOSE_TIME','<=',date('Y-m-d 23:59:59')]])->field('MO_DOC,STATUS')->select();
        $smts['data']=$smts->toArray();
        $smts['data']=$this->status($smts['data']);
        $smts['count']=count($smts['data']);
        //dip
        $dips=(new T_PM_MO_BASE())->where([['PRODUCT_STEP','like','%THT%'],['STATUS','in',[3,5]],['CLOSE_TIME','>=',date('Y-m-d 00:00:00')],['CLOSE_TIME','<=',date('Y-m-d 23:59:59')]])->field('MO_DOC,STATUS')->select();
        $dips['data']=$dips->toArray();
        $dips['data']=$this->status($dips['data']);
        $dips['count']=count($dips['data']);
        //总装
        $zzs=(new T_PM_MO_BASE())->where([['PRODUCT_STEP','like','%总装%'],['STATUS','in',[3,5]],['CLOSE_TIME','>=',date('Y-m-d 00:00:00')],['CLOSE_TIME','<=',date('Y-m-d 23:59:59')]])->field('MO_DOC,STATUS')->select();
        $zzs['data']=$zzs->toArray();
        $zzs['data']=$this->status($zzs['data']);
        $zzs['count']=count($zzs['data']);
        //今日不良清单
        $Ng=(new T_PM_MO_BASE())->where([['SCRAP_QTY','<>',0],['create_time','>=',date('Y-m-d 00:00:00')],['create_time','<=',date('Y-m-d 23:59:59')]])->field('MO_DOC,OUTPUT_QTY,SCRAP_QTY')->select();
        $Ng['data']=$Ng->toArray();
        $Ng['count']=count($Ng);
        $sum=0;
        $ngcount=0;
        foreach ($Ng['data'] as $k=>$v){
            if(!empty($v['OUTPUT_QTY'])){
                //不良比例
                $Ng[$k]['proportion']=intval($v['SCRAP_QTY']/$v['OUTPUT_QTY'])*100;
                //不良总数和总比例
                $sum+=$v['OUTPUT_QTY'];
                $ngcount+=$v['SCRAP_QTY'];
            }else{
                $Ng[$k]['proportion']=0;
            }
        }
        $Ng['SCRAP_QTY']=$ngcount;
        if($sum!=0){
            $Ng['proportion']=intval($ngcount/$sum)*100;
        }else{
            $Ng['proportion']=0;
        }
        return view('/Ts',['smtData'=>$smt,'dipData'=>$dip,'zzData'=>$zz,'smtsData'=>$smts,'dipsData'=>$dips,'zzsData'=>$zzs,'Ng'=>$Ng]);
    }

    public function status($data=[]){
        foreach ($data as $k=>$v){
            switch($v['STATUS']){
                case 1: $data[$k]['STATUS']='开立';break;
                case 2: $data[$k]['STATUS']='投产';break;
                case 3: $data[$k]['STATUS']='关结';break;
                case 4: $data[$k]['STATUS']='暂停';break;
                case 5: $data[$k]['STATUS']='强制关结';break;
            }
        }
        return $data;
    }

    /**
     * 首件巡检
     */
    public function checks(){
        //今日首检清单
        $list1=(new T_QM_FIRST_INFO())
            ->where([['create_time','>=',date('Y-m-d 00:00:00')],['create_time','<=',date('Y-m-d 23:59:59')]])
            ->field('DOC_NO,MO_NUMBER,PROJECT_NO,RESULT_STATUS,STATUS,MODEL_CODE,FIRST_QTY,SEND_QTY,GROUP_NAME')
            ->select()->toArray();
        $list1['data']=$list1;
        $list1['count']=count($list1['data']);
        $num=0;
        foreach ($list1['data'] as $k=>$v){
            //判断首检单状态
            switch ($v['STATUS']){
                case 1:$list1['data'][$k]['STATUS']='开立';break;
                case 2:$list1['data'][$k]['STATUS']='检验中';break;
                case 3:$list1['data'][$k]['STATUS']='检验完成';break;
            }
            if($v['RESULT_STATUS']=='OK'){
                $num++;
            }
        }
        //首检数量，通过率
        if($list1['count']!=0){
            $list1['hg']=intval($num/$list1['count'])*100;
        }else{
            $list1['hg']=0.00;
        }
        //今日巡检单
        $list2=(new T_QM_IPQC_DOC())->alias('a')
            ->leftjoin('T_QM_IPQC_DOC_ITEM b','a.DOC_NO=b.DOC_NO')
            ->where([['CHECK_TIME','>=',date('Y-m-d 00:00:00')],['CHECK_TIME','<=',date('Y-m-d 23:59:59')]])
            ->field('a.DOC_NO,a.PROJECT_NO,a.MODEL_CODE,STATUS,a.AREA_SN,b.TEST_ITEM_NAME,b.TEST_CONTENT,b.TEST_RESULT,a.CHECK_TIME')
            ->select()->toarray();
        $list2['data']=$list2;
        $list2['count']=count($list2['data']);
        $num2=0;
        foreach ($list2['data'] as $k=>$v){
            //判断首检单状态
            switch ($v['STATUS']){
                case 1:$list2['data'][$k]['STATUS']='开立';break;
                case 2:$list2['data'][$k]['STATUS']='检验中';break;
                case 3:$list2['data'][$k]['STATUS']='检验完成';break;
            }
            switch ($v['TEST_RESULT']){
                case 1:$list2['data'][$k]['TEST_RESULT']='合格';break;
                case 2:$list2['data'][$k]['TEST_RESULT']='不合格';break;
            }

            //不良数量，检验结果不为OK
            if($v['TEST_RESULT']!='1')$num2++;
        }
        $list2['NgNum']=$num2;
        //昨日巡检单
        $list3=(new T_QM_IPQC_DOC())->alias('a')
            ->leftjoin('T_QM_IPQC_DOC_ITEM b','a.DOC_NO=b.DOC_NO')
            ->where([['CHECK_TIME','>=',date('Y-m-d 00:00:00',strtotime('-1 day'))],['CHECK_TIME','<=',date('Y-m-d 23:59:59',strtotime('-1 day'))]])
            ->field('a.DOC_NO,a.PROJECT_NO,a.MODEL_CODE,STATUS,a.AREA_SN,b.TEST_ITEM_NAME,b.TEST_CONTENT,b.TEST_RESULT,a.CHECK_TIME')
            ->select()->toarray();
        $list3['data']=$list3;
        $list3['count']=count($list3['data']);
        $num3=0;
        foreach ($list3['data'] as $k=>$v){
            //判断巡检单状态
            switch ($v['STATUS']){
                case 1:$list3['data'][$k]['STATUS']='开立';break;
                case 2:$list3['data'][$k]['STATUS']='检验中';break;
                case 3:$list3['data'][$k]['STATUS']='检验完成';break;
            }
            //判断巡检单结果
            switch ($v['TEST_RESULT']){
                case 1:$list3['data'][$k]['TEST_RESULT']='OK';break;
                case 0:$list3['data'][$k]['TEST_RESULT']='NO';break;
            }
            //不良数量，检验结果不为OK
            if($v['TEST_RESULT']!='1')$num3++;
        }
        $list3['NgNum']=$num3;
        return view('/Check',['data1'=>$list1,'data2'=>$list2,'data3'=>$list3]);
    }


}
